<template>
  <el-dialog id="edit-dialog" :title="$t('channel.channelBranch.btn.generate')" :visible.sync="showState" :destroy-on-close="true" :before-close="handleClose" :close-on-click-modal="false">
    <el-form ref="formValidate" :model="formValidate" :rules="rules" label-width="150px" class="demo-ruleForm">
      <el-form-item>
        {{ $t('channel.prompt.unusedInvitationCode') }}
        <span>{{ quantity }}</span>
        {{ $t('channel.prompt.piece') }}
      </el-form-item>
      <el-form-item :label="$t('channel.prompt.generateNumber')" prop="generateNumber">
        <el-input-number
          v-model="formValidate.generateNumber"
          :controls="false"
          :max="10000000000000"
          :placeholder="$t('channel.prompt.generate')"
          style="width: 100%"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">{{ $t('common.operate.cancel') }}</el-button>
      <el-button type="primary" :loading="submitLoading" @click="handleSubmit">{{ $t('common.ex') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { generate } from '@/api/operation'
export default {
  name: 'ChannelInvitationCodeDialog',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    quantity: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      submitLoading: false,
      formValidate: {
        generateNumber: null
      },
      rules: {
        generateNumber: [{ required: true, message: this.$t('channel.prompt.generateNumber') + this.$t('common.null'), trigger: 'blur' }]
      }
    }
  },
  computed: {
    showState: {
      get: function() {
        return this.showDialog
      },
      set: function() {
      }
    }
  },
  methods: {
    handleClose() {
      this.$emit('change-dialog', false)
      this.formValidate.generateNumber = null
    },
    handleSubmit() {
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          const data = {
            channelId: this.$route.query.id,
            count: this.formValidate.generateNumber
          }
          generate(data)
          this.handleClose()
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
span{
  color: #fb0036;
}
</style>
